package com.wing.robot.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.robot.dao.RobotTaskDetailDao;
import com.wing.robot.model.entity.RobotTaskDetail;
import com.wing.robot.model.request.RobotTaskDetailForm;
import com.wing.robot.model.request.RobotTaskForm;
import com.wing.robot.model.response.RobotTaskDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 复合机器人任务详细记录-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class RobotTaskDetailServiceImpl extends ServiceImpl<RobotTaskDetailDao, RobotTaskDetail> implements RobotTaskDetailService {

    @Value("${robot.url}")
    private String url;

    @Override
    public int add(RobotTaskDetailForm robotTaskDetailForm) {

        //校验

        RobotTaskDetail robotTaskDetail =new RobotTaskDetail();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(RobotTaskDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(robotTaskDetailForm, robotTaskDetail, copyOptions);

        return baseMapper.insert(robotTaskDetail);
    }

    @Override
    public List<RobotTaskDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<RobotTaskDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<RobotTaskDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public RobotTaskDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public String getUniqueCode() {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String day=simpleDateFormat.format(new Date());
        String uniqueCode = "Robot"+day+RandomStringUtils.randomAlphanumeric(15);;
        return uniqueCode;
    }

    @Override
    public JsonResult doTask(RobotTaskForm robotTaskForm) {
        //获取运单唯一编号
        String uniqueCode = getUniqueCode();
        //拼合单
        Map<String,Object> param = new HashMap();
        param.put("loadPostAction",new HashMap(){{put("configId","loadPost");}});
        param.put("unloadPostAction",new HashMap(){{put("configId","unloadPost");}});
        /*回调配置
          "loadPostAction": {     			// 取货后回调（可选）
                 "configId": "LoadReport"	// 替换为模型文件中配置的HttpPost名称
             },
            "unloadPostAction": {					// 放货后回调（可选）
            "configId": "UnloadReport"// 替换为模型文件中配置的HttpPost名称
                 },
        * */
        param.put("id",uniqueCode);
        param.put("fromLoc",robotTaskForm.getInfoList().get(0).getAgvFrom());
        param.put("toLoc",robotTaskForm.getInfoList().get(0).getAgvTo());
        RobotTaskDetail robotTaskDetail = new RobotTaskDetail();
        robotTaskDetail.setCode(uniqueCode);
        robotTaskDetail.setAgvFrom(robotTaskForm.getInfoList().get(0).getAgvFrom());
        robotTaskDetail.setAgvTo(robotTaskForm.getInfoList().get(0).getAgvTo());
        robotTaskDetail.setTaskId(robotTaskForm.getId());
        baseMapper.insert(robotTaskDetail);
        if (robotTaskForm.getInfoList().size()>1){
            RobotTaskDetail robotTaskDetailOther = null;
            for (int i = 1; i < robotTaskForm.getInfoList().size() ; i++) {
                param.put("fromLoc"+(i+1),robotTaskForm.getInfoList().get(i).getAgvFrom());
                param.put("toLoc"+(i+1),robotTaskForm.getInfoList().get(i).getAgvTo());
                robotTaskDetailOther = new RobotTaskDetail();
                robotTaskDetailOther.setCode(uniqueCode);
                robotTaskDetailOther.setAgvFrom(robotTaskForm.getInfoList().get(i).getAgvFrom());
                robotTaskDetailOther.setAgvTo(robotTaskForm.getInfoList().get(i).getAgvTo());
                robotTaskDetailOther.setTaskId(robotTaskForm.getId());
                baseMapper.insert(robotTaskDetailOther);
            }
        }
        String res = HttpUtil.post(url+"/setOrder",param);
        JSONObject result = new JSONObject(res);
        if (result.get("code").equals(200)){
            return JsonResult.success("下发成功");
        }
        return JsonResult.fail("下发失败");



    }















}
